﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Sparrow.Reflection.Samples.Benchmarks
{
    class Program
    {
        static void Main(string[] args)
        {
             int times = 1000000;
             var instance = new Test();

             TestInvokeMethod(instance, times);

        }

        static void TestInvokeMethod(Test instance,int times)
        {
            var watch = new Stopwatch();





            watch.Start();

            //directly invoke

            for (int i = 0; i < times; i++)
            {
                instance.CallMe();
            }

            watch.Stop();

            Console.WriteLine("Directly invoke                   : {0}", watch.ElapsedMilliseconds);

            watch.Reset();

            watch.Start();

            //build-in reflection
            var type = instance.GetType();
            var method = type.GetMethod("CallMe");
            for (int i = 0; i < times; i++)
            {
                method.Invoke(instance, null);    
            }

            watch.Stop();

            Console.WriteLine("build-in reflection                   : {0}", watch.ElapsedMilliseconds);

            watch.Reset();

            //.Proxy()
            watch.Start();

            var method3 = instance.Proxy().Methods["CallMe"];
            for (int i = 0; i < times; i++)
            {
                method3.Invoke(null);
            }

            watch.Stop();

            Console.WriteLine("instance.Proxy().Methods[\"CallMe\"]: {0}", watch.ElapsedMilliseconds);
            watch.Reset();

            //.Metadata()
            watch.Start();

            var method2 = instance.Metadata().Methods["CallMe"];
            for (int i = 0; i < times; i++)
            {
                method2.Invoke(instance);
            }

            watch.Stop();

            Console.WriteLine("instance.Metadata().Methods[\"CallMe\"]: {0}", watch.ElapsedMilliseconds);
            watch.Reset();


         



        

            //.Dynamic()
            watch.Start();


            for (int i = 0; i < times; i++)
            {
                instance.Dynamic().CallMe();
            }

            watch.Stop();

            Console.WriteLine("instance.Dynamic().CallMe(): {0}", watch.ElapsedMilliseconds);
            watch.Reset();


           
            
        }
    }

    class Test
    {
        public void CallMe()
        {
        }

        public void SayHello(string name)
        {
        }
    }
}
